<template>
  <div class="password-container">
    <el-card class="password-card">
      <div class="password-header">
        <i class="el-icon-lock"></i>
        <h2>修改登录密码</h2>
      </div>

      <el-form
          class="password-form"
          :model="form"
          :rules="rules"
          ref="passwordForm"
      >
        <el-form-item prop="password">
          <el-input
              v-model="form.password"
              type="password"
              placeholder="请输入6-10位新密码"
              clearable
              show-password
          >
            <template #prefix>
              <i class="el-icon-key"></i>
            </template>
          </el-input>
        </el-form-item>

        <el-button
            type="primary"
            class="submit-btn"
            :loading="loading"
            @click="submit"
        >
          <i class="el-icon-check"></i>
          {{ loading ? '提交中...' : '确认修改' }}
        </el-button>
      </el-form>

      <div class="security-tips">
        <i class="el-icon-warning"></i>
        <span>建议使用字母、数字和符号组合的复杂密码</span>
      </div>
    </el-card>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  data() {
    const validatePassword = (rule, value, callback) => {
      if (!value) {
        callback(new Error('密码不能为空'));
      } else if (value.length < 6) {
        callback(new Error('密码长度不能少于6位'));
      } else {
        callback();
      }
    };

    return {
      form: { password: '' },
      loading: false,
      rules: {
        password: [
          { validator: validatePassword, trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    submit() {
      this.$refs.passwordForm.validate(valid => {
        if (valid) {
          this.loading = true;
          request.post("/password", this.form)
              .then(res => {
                if (res.code === '0') {
                  this.$message.success({
                    message: '修改成功，请重新登录',
                    duration: 1500,
                    onClose: () => {
                      localStorage.removeItem("user");
                      this.$router.push("/login");
                    }
                  });
                }
              })
              .finally(() => this.loading = false);
        }
      });
    }
  }
}
</script>

<style scoped lang="scss">
.password-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 70vh;
}

.password-card {
  width: 100%;
  max-width: 400px;
  border-radius: 15px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.1);

  ::v-deep .el-card__body {
    padding: 30px;
  }
}

.password-header {
  text-align: center;
  margin-bottom: 30px;

  i {
    font-size: 40px;
    color: #409EFF;
    margin-bottom: 10px;
  }

  h2 {
    color: #303133;
    margin: 10px 0;
    font-size: 24px;
  }
}

.password-form {
  .el-input {
    ::v-deep .el-input__inner {
      height: 45px;
      border-radius: 25px;
      padding-left: 40px;
    }

    i {
      font-size: 18px;
      color: #409EFF;
    }
  }
}

.submit-btn {
  width: 100%;
  height: 45px;
  font-size: 16px;
  border-radius: 25px;
  margin-top: 20px;
  transition: all 0.3s ease;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 15px rgba(64, 158, 255, 0.3);
  }

  i {
    margin-right: 8px;
  }
}

.security-tips {
  margin-top: 20px;
  padding: 15px;
  background: #f0faff;
  border-radius: 8px;
  display: flex;
  align-items: center;
  gap: 8px;

  i {
    color: #409EFF;
  }

  span {
    color: #606266;
    font-size: 13px;
  }
}

@media (max-width: 768px) {
  .password-card {
    margin: 20px;
    border-radius: 12px;
  }

  .password-header h2 {
    font-size: 20px;
  }
}
</style>